#ifndef TcpConnector_h__
#define TcpConnector_h__
#include <libuv/uv.h>
#include <string>
#include <sstream>
#include <memory>
#include <uvw/uvw.hpp>
#include <functional>
#include <google/protobuf/message.h>
#include "session.h"
#include "../myrpc/myrpc.pb.h"

namespace myrpc
{
	namespace mynet
	{	
		class TcpConnector;
		struct ClientPackage
		{	
			myrpc::Response body;
			std::shared_ptr<TcpConnector> connect;
		};

		typedef std::shared_ptr<ClientPackage>  ClientPackagePtr;

		class TcpConnector : public std::enable_shared_from_this<TcpConnector>
		{
		public:
			TcpConnector() = default;
			~TcpConnector() = default;
			int Connect(std::string ip, int port);
			void Send(std::string data , int len);
			void Setpackgereachhandle(std::function<void(std::shared_ptr<ClientPackage>)>  packgereachhandle) {

				packgereachhandle_ = packgereachhandle;
			}
		private:
			std::function<void(std::shared_ptr<TcpConnector>)>  connecthandle_;
			std::function<void(std::shared_ptr<TcpConnector>)>  closehandle_;
			std::function<void(std::shared_ptr<TcpConnector>)>  errorhandle_;
			std::function<void(std::shared_ptr<TcpConnector>)>  writehandle_;
			std::function<void(std::shared_ptr<ClientPackage>)>  packgereachhandle_;

			int bodylenght_;
			std::string in_;
			std::string out_;
			std::string ip_;
			int port_;

			std::shared_ptr<uvw::TcpHandle>  _tcphandler;
			std::shared_ptr<uvw::Loop> loop_;

		};


	}
}

#endif



